//定义系统显示的图标
SAM_GIF = "sam.gif";
EPD_GIF = "epd.gif";
WFS_GIF = "wfs.gif";
SCG_GIF = "scg.gif";
_GIF = "default.gif";
MOA_GIF = "office.gif";
CUS_GIF = "cus.gif";
SPM_GIF = "spm.gif";
MFA_GIF = "mfa.gif";
SAM_GIF = "sam.gif";
EPD_GIF = "epd.gif";
WFS_GIF = "wfs.gif";
SCG_GIF = "scg.gif";
DEFAULT_GIF = "images/login/default.gif";
MOA_GIF = "office.gif";
CUS_GIF = "cus.gif";
SPM_GIF = "spm.gif";
MFA_GIF = "mfa.gif";

root = 0;  //虚拟根
sum_level = 1; //菜单层数
sum_show = 30; //菜单显示数
space = '<img src="images/login/line.gif" vspace="0" hspace="1" border=\"0\">'; //菜单缩进空格数

function menuAction(id) //菜单操作处理，判断该进行展开还是折叠
{
	var idx = findIdx(id);
	if (idx<0) return;
	var m_menu = menus[idx];
	if (m_menu.isLeaf)
	{
		return;
	}else
	{
		if (m_menu.expanded)
		{
			foldMenu(m_menu);
		}else
		{
			expandMenu(m_menu);
		}
		m_menu.expanded = !(m_menu.expanded);
	}
}

function findIdx(id) //查找位置号
{
	return id-1;
}

function expandMenu(m_menu) //展开单级菜单
{
	var id = m_menu.id;
	var idx = findIdx(id);
	for(var i=idx+1;i<menus.length;i++)
	{
		if (menus[i].p_id==id)
		{
			showMenu(menus[i]);
		}
	}
}

function showMenu(m_menu) //显示某个菜单
{
	id = m_menu.id;
	obj = eval('menu_'+id);

	obj.style.display = "block";
	obj.style.visibility = "visible";

}

function foldMenu(m_menu) //折叠某个菜单
{
	var idx = findIdx(m_menu.id);
	for(var i=idx+1;i<menus.length;i++)
	{
		if (menus[i].p_id==m_menu.id)
		{
			hideMenu(menus[i]);
		}
	}
}

function hideMenu(m_menu) //隐藏菜单，递归式
{
	m_menu.expanded = false;
	var id = m_menu.id;
	var obj = eval("menu_"+id);

	obj.style.display = "none";
	obj.style.visibility = "hidden";

	// hide child
	var idx = findIdx(id);
	for(var i=idx+1;i<menus.length;i++)
	{
		if (menus[i].p_id==id)
		{
			hideMenu(menus[i]);
		}
	}
}

function foldAll(m_level) //折叠超过控制的层数
{
	for(var i=0;i<menus.length;i++)
	{
		if (menus[i].level == m_level)
		{
			menus[i].expanded = false;
			foldMenu(menus[i]);
		}
	}
}

function Menu(id,code,name,link,p_id,help,isLeaf,level) //菜单定义
{
	this.id = id;
	this.code = code;
	this.name = name;
	this.link = link;
	this.p_id = p_id;
	this.help = help;
	this.isLeaf = isLeaf;
	this.level = level;
	this.expanded = true;
	if (level>sum_level) sum_level = level;
	return this;
}

function init() //初始化
{
	for(var i=0;i<menus.length;i++)
	{
		if (menus[i].p_id==root)
		{
			formMenu(menus[i],'');
		}
	}
	if (sum_level>1 && menus.length>sum_show) //多层且超过显示数
	{
		showLevel = getShowLevel();
		//alert(showLevel);
		if (showLevel<1) showLevel = 1;
		foldAll(showLevel);
	}
}

function getShowLevel() //获得显示层数
{
	tmp_sum = 0; //统计菜单数
	for(i=1;i<=sum_level;i++)
	{
		for(j=0;j<menus.length;j++)
		{
			if (menus[j].level==i && menus[i].p_id>-1) tmp_sum++;
			if (tmp_sum >sum_show) return (i-1);
		}
	}
	return 0;
}

function formMenu(m_menu,m_str) //形成菜单
{
	var strImg = "";  //图片
	var lastOne = lastMenu(m_menu);

	if (m_menu.isLeaf)
	{
		if (lastOne)
		{
			strImg = "joinbottom.gif";
		}else
		{
			strImg = "join.gif";
		}
		printMenu("<div class=\"menu"+m_menu.level+"\" id=\"menu_"+m_menu.id+"\"><nobr>"+m_str+"<a href=\""+m_menu.link+"\" target=right><img src=\"images/login/"+strImg+"\" height=\"16\" width=\"20\" border=\"0\" vspace=\"0\" hspace=\"1\">"+m_menu.name+"</a></nobr></div>");
		return ;
	}else
	{
		if (m_menu.level==1)
		{
			// decide which picture is to be used
			if (m_menu.code.toLowerCase().substring(0,7)=="sam_sam")
			{
				strImg = SAM_GIF;

			}else if (m_menu.code.toLowerCase().substring(0,7)=="wfs_wfs")
			{
				strImg = WFS_GIF;

			}else if (m_menu.code.toLowerCase().substring(0,7)=="scg_scg")
			{
				strImg = SCG_GIF;

			}else if (m_menu.code.toLowerCase().substring(0,7)=="epd_epd")
			{
				strImg = EPD_GIF;

			}else if (m_menu.code.toLowerCase().substring(0,7)=="moa_moa")
			{
				strImg = MOA_GIF;

			}else if (m_menu.code.toLowerCase().substring(0,7)=="cus_cus")
			{
				strImg = CUS_GIF;

			}else if (m_menu.code.toLowerCase().substring(0,7)=="spm_spm")
			{
				strImg = SPM_GIF;

			}else if (m_menu.code.toLowerCase().substring(0,7)=="mfa_mfa")
			{
				strImg = MFA_GIF;

			}else
			{
				strImg = _GIF;
			}

			printMenu("<div class=\"menu"+m_menu.level+"\" id=\"menu_"+m_menu.id+"\" onClick=\"menuAction("+m_menu.id+")\"><nobr><img src=\"images/login/"+strImg+"\" height=\"19\" width=\"20\" border=\"0\" vspace=\"0\" hspace=\"6\">"+m_menu.name+"</nobr></div>");
		}else
		{
			if (hasChild(m_menu))
			{
				if (lastOne)
				{
					strImg = "plusbottom.gif";
				}else
				{
					strImg = "plus.gif";
				}
				//strImg = "down_arrow.gif";
			}else
			{
				if (lastOne)
				{
					strImg = "minusbottom.gif";
				}else
				{
					strImg = "minus.gif";
				}
				//strImg = "left_arrow.gif";
			}
			printMenu("<div class=\"menu"+m_menu.level+"\" id=\"menu_"+m_menu.id+"\" onClick=\"menuAction("+m_menu.id+")\"><nobr>"+m_str+"<img src=\"images/login/"+strImg+"\" height=\"16\" width=\"20\" border=\"0\" vspace=\"0\" hspace=\"1\">"+m_menu.name+"</nobr></div>");
		}

		// show picture or space
		var indentStr = '';
		if (m_menu.level==1 || lastOne)
		{
			indentStr = '&nbsp;&nbsp;&nbsp;&nbsp;';
		}else
		{
			indentStr = space;
		}

		//show children
		var idx = findIdx(m_menu.id);
		for(var i=idx+1;i<menus.length;i++)
		{
			if (menus[i].p_id==m_menu.id)
			{
				formMenu(menus[i], m_str+indentStr);
			}
		}
	}
}

function printMenu(str) //打印
{
	document.writeln(str);
}

//判断是否该级最后一菜单
function lastMenu(m_menu)
{
	idx = findIdx(m_menu.id);
	for(var i=idx+1;i<menus.length;i++)
	{
		if (menus[i].p_id==m_menu.p_id) return false;
	}
	return true;
}
//是否有子
function hasChild(m_menu)
{
	if (m_menu.isLeaf) return false;

	id = m_menu.id;
	idx = findIdx(id);
	for(var i=idx+1;i<menus.length;i++)
	{
		if (menus[i].p_id==id) return true;
	}
	return false;
}

// 现在没使用
function getMenuImage(m_menu)
{

}
//得到菜单的顶端位置
function getMenuTop(m_menu)
{

}
menus = new Array();